package com.example.springdemo.controller;

import com.example.springdemo.entity.Emp;
import com.example.springdemo.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: Hu Shan
 * @date: 2023/3/22 20:58
 * @description: TODO
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "mydemo", description = "员工信息")
@Slf4j
public class EmpController {

    @Autowired
    EmpService empService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取员工信息", notes = "输入id")
//    @ApiIgnore
    public ResponseEntity<Emp> getEmpInfo(@PathVariable @ApiParam(value = "员工工号", required = true) int id){
        log.info("getEmpInfo " + id);
        return ResponseEntity.ok(empService.getEmpById(id));
    }

    @PostMapping("/insert")
    public String insertEmp(@RequestBody Emp emp) {
        log.info("insertEmp " + emp);
        empService.insertEmp(emp);
        return "success";
    }

    @PutMapping("/update")
    public String updateEmp(@RequestBody Emp emp) {
        log.info("updateEmp " + emp);
        if (empService.updateEmp(emp) == 0) {
            return "fail";
        }
        return "success";
    }

    @DeleteMapping("/delete/{id}")
    public String deleteEmp(@PathVariable int id) {
        log.info("deleteEmp " + id);
        if (empService.deleteEmp(id) == 0) {
            return "fail";
        }
        return "success";
    }

}
